﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using SX_MesProject.Dtos.LJQDtos;
using SX_MesProject.Dtos;
using SX_MesProject.ProcessManage;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static SX_MesProject.HttpClientHelper;

namespace SX_MesProject
{
    public partial class Process_Management : DevExpress.XtraEditors.XtraForm
    {
        public Process_Management()
        {
            InitializeComponent();
        }

        private void Process_Management_Load(object sender, EventArgs e)
        {

            //gridView1
            ////自带搜索框
            //gridView1.OptionsFind.AlwaysVisible = true;
            ////单条选中且不可编辑 
            //gridView1.OptionsBehavior.Editable = false;
            //gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            //gridControl1.UseEmbeddedNavigator = true;
            //单击事件
            gridView1.RowClick += GridView_RowClick;

            gridView2.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            gridView2.RowClick += GridView_RowClick2;

#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法

            //gridView1.RowClick += GridView_RowClick;

            Databinds();
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
        }
        private async Task Databinds()
        {
            try
            {
                var list = await HttpClientHelper.RequestUrl<string, ResponseDto<List<LocationMesWarehouse>>>("https://localhost:44556/", "GetMesWarehouseList", MethodType.GET);
                this.gridControl1.DataSource=list.Data;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        private void GridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            // 这里是点击行时要执行的逻辑
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                // 获取点击行的数据
                object rowData = view.GetRow(e.RowHandle);
                // 进行相应的处理
                string rowData1 = JsonConvert.SerializeObject(rowData);
                var list = Newtonsoft.Json.JsonConvert.DeserializeObject<WorkingProcedureList>(rowData1);
                ProcedureBindEquipment(list.Id);
            }
        }

        private void GridView_RowClick2(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            // 这里是点击行时要执行的逻辑
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                // 获取点击行的数据
                object rowData = view.GetRow(e.RowHandle);
                // 进行相应的处理
                string rowData1 = JsonConvert.SerializeObject(rowData);
                var list = Newtonsoft.Json.JsonConvert.DeserializeObject<InventoryLocationListDto>(rowData1);
                var res = list;
            }
        }
        public int Value { get; set; }
        private async void ProcedureBindEquipment(int warehousequeryId)
        {
            try
            {
                WareHouseQueryId wareHouseQueryId = new WareHouseQueryId();
                wareHouseQueryId.WarehouseId = warehousequeryId;
                var list = await HttpClientHelper.RequestUrl<WareHouseQueryId, ResponseDto<List<InventoryLocationListDto>>>("https://localhost:44556/", "GetInventoryLocationList", MethodType.POST, wareHouseQueryId);
                this.Value = warehousequeryId;
                this.gridControl2.DataSource = list.Data;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            InventoryLocationAdd locationAdd = new InventoryLocationAdd();
            locationAdd.ShowDialog();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (this.Value < 1)
            {
                MessageBox.Show("请先选择需要绑定的工序");
            }
            else
            {
                ProcedureBindAdd procedureBindAdd = new ProcedureBindAdd(this.Value);
                procedureBindAdd.OnLoadData += Databinds;
                procedureBindAdd.ShowDialog();
            }
        }
    }
    public class WareHouseQueryId
    {
        public int WarehouseId { get; set; }
    }
    //库位显示
    public class InventoryLocationListDto
    {
        public int Id { get; set; }
        public int WarehouseId { get; set; }
        public string LocationNo { get; set; }
        public int Sort { get; set; }
        public int UseState { get; set; }
        public string TypeName { get; set; }
        public int LocationState { get; set; }
        public int BindBomId { get; set; }
        public string BindBomStr { get; set; }
        public string Remark { get; set; }
        public DateTime Create_Time { get; set; }
    }
}